package com.zmj.controller;

import java.util.List;


import javax.servlet.http.HttpServletRequest;

import com.alibaba.fastjson.JSON;
import com.zmj.anno.MyAjax;
import com.zmj.anno.RequestPath;
import com.zmj.constant.CommConstant;
import com.zmj.entity.Goods;
import com.zmj.entity.GoodsType;
import com.zmj.entity.Page;
import com.zmj.service.IGoodsService;
import com.zmj.service.impl.GoodsServiceImpl;
import com.zmj.utils.StringUtil;

public class GoodsController {

	IGoodsService goodsServiceImpl=new GoodsServiceImpl();
	
	@RequestPath("/index.shop")
	public String index (HttpServletRequest request) {
		//查询商品列表，并去前台展示
		Goods goods=new Goods();
		goods.setPage(new Page(0,6));
		
		List<Goods> goodlist=goodsServiceImpl.selGoods(goods);
		
		request.setAttribute("goodlist",goodlist);
		return "index.jsp";
	}
	//根据对应的条件来查询商品
		@RequestPath("/query.shop")
		public String queryGoods(HttpServletRequest request){
			
			Goods ge = new Goods();
			
			String type = request.getParameter("gtype");
			
			String tnew = request.getParameter("gnew");
			String tdis = request.getParameter("gdiscount");
			String start = request.getParameter("startprice");
			String end = request.getParameter("endprice");
		
			//根据商品类别查找
			if(!StringUtil.isnull(type)){
				ge.setGtype(Integer.parseInt(type));
			}
			
			//查找所有的新品
			if(!StringUtil.isnull(tnew)){
				ge.setGnew(1);
			}
			
			//查找折扣商品
			if(!StringUtil.isnull(tdis)){
				ge.setGdiscount(Float.parseFloat(tdis));
			}
			
			//最小价格
			if(!StringUtil.isnull(start)){
				ge.setStartprice(Float.parseFloat(start));
			}
			
			//最大价格
			if(!StringUtil.isnull(end)){
				ge.setEndprice(Float.parseFloat(end));
			}
			
			/////////////////////////////////分页代码start
			String cp = request.getParameter("currentPage");
			String pageSize = request.getParameter("pageSize");
			
			int currentPage;
			if(StringUtil.isnull(cp)){
				currentPage = CommConstant.ONE;
			}else{
				currentPage = Integer.parseInt(cp);
			}
			
			Page pe = new Page();
			pe.setCurrentPage(currentPage);
			
			if(!StringUtil.isnull(pageSize)){
				pe.setPageSize(Integer.parseInt(pageSize));
			}
			
			ge.setPage(pe);//分页条件,放到查询条件里面去
			
			/////////////////////////////////////分页代码end
			
			
			List<Goods> goodsList = goodsServiceImpl.selGoods(ge);
			
			request.setAttribute("goodsList", goodsList);
			
			return "list.jsp";
		}
		
		@RequestPath("/queryType.shop")
		@MyAjax
		public String queryType(HttpServletRequest request) {
			
			List<GoodsType> list=goodsServiceImpl.queryType();
			
			String result = JSON.toJSONString(list);
			return result;
		}
}
